<template>
	<view>
		<view class="editNickNameBox">
			<view class="tittle">
				{{isAnnouncement?'群公告':'群备注'}}
			</view>
			<view class="editBox">
				<image class="avatar" :src="avatar" mode=""></image>
				<input class="editInput" type="text" v-model="nickname" :placeholder="isAnnouncement?'公告':'备注'" />
			</view>
		</view>
		<view class="saveBtn" @click="editNickname">
			完成
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname:'',
				avatar:'',
				groupId:'',
				isAnnouncement: false,
				userId:'',
				selfInfo:''
			}
		},
		onLoad(options) {
			this.selfInfo = uni.getStorageSync('selfInfo')
			this.groupId = options.groupId
			this.isAnnouncement = options.isAnnouncement == 1
			this.avatar = JSON.parse(uni.getStorageSync('avatar'))[0]
			this.userId = uni.getStorageSync('userId').toString()
		},
		methods: {
			editNickname(){
				if(!this.nickname) {
					uni.showToast({
						icon:"none",
						title:"内容不能为空"
					})
					return
				}
				if(this.isAnnouncement){
					let data = {
						GroupId:this.groupId,
						Notification:this.nickname,
					}
					this.$utils.postrequest("/api/tim/update_group_notification",data,res=>{
						if(res.code === 200){
							uni.showToast({
								icon:"success",
								title:"修改成功1",
								success:res=>{
									uni.navigateBack({
										delta:1
									})
								}
							})
						}
						else{
							uni.showToast({
								icon:"none",
								title:res.message
							})
						}
					})
					return
				}
				let data = {
					GroupId:this.groupId,
					Member_Account:this.userId,
					NameCard:this.nickname,
				}
				this.$utils.postrequest("/api/tim/update_member_group_notice",data,res=>{
					if(res.code === 200){
						this.selfInfo.nameCard = this.nickname
						uni.setStorageSync('selfInfo',this.selfInfo)
						uni.showToast({
							icon:"success",
							title:"修改成功2"
						})
					}
					else{
						// console.log(res)
					}
				})
			}
		}
	}
</script>

<style>
	
	.tittle {
		line-height: 160rpx;
		width: 100%;
		border-bottom: 1rpx solid rgb(200,200,200);
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
	}
	
	.editNickNameBox {
		padding: 42rpx;
		margin-top: 100rpx;
		height: 300rpx;
		display: flex;
		flex-direction: column;
	}
	
	.avatar {
		height: 100rpx;
		width: 100rpx;
		border-radius: 15rpx;
	}
	
	.editBox {
		margin-top: 20rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	
	.editInput {
		margin-left: 20rpx;
		height: 100rpx;
	}
	
	.saveBtn {
		margin: 0 auto;
		width: 180rpx;
		height: 80rpx;
		background-color: #007AFF;
		border-radius: 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #FFFFFF;
		margin-top: 120rpx;
	}
</style>
